package Demos;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.seimicrawler.xpath.JXDocument;
import org.seimicrawler.xpath.JXNode;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.List;

/**
 * 获取北京新发地
 */
public class Main {

    public static void main(String[] args) throws Exception {
        HttpGet httpGet2 = new HttpGet("http://www.xinfadi.com.cn/marketanalysis/0/list/1.shtml");
        // 1. 打开浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 2. 模拟发送请求
        HttpGet httpGet1 = new HttpGet("http://whbsz.com.cn/Price.aspx?PageNo=1");

        // 3. 执行操作, 获取响应数据
        CloseableHttpResponse response1 = httpClient.execute(httpGet1);

        // 4. 获取响应
        String html = EntityUtils.toString(response1.getEntity());

        // 5. 执行你需要的操作
//        System.out.println(html);

        // 保存到本地
/*        File file = new File("src/main/resources/html/武汉白沙洲农产品.html");
        PrintStream ps = new PrintStream(new FileOutputStream(file));
        ps.println(html);*/
        // 获取 品名，产地/规格，最高价，最低价，平均价
        JXDocument doc = JXDocument.create(html);
        // 完全使用xpath
//        List<JXNode> trs = doc.selN("/body//tr[contains(@class, 'Center) and contains(@class, 'P14')]");

        // 利用CSS选择器 + xpath + 正则
        Elements tr1s = doc.selNOne("/body").asElement().select("tr.center");
        String[] names = {"品名", "产地/规格", "最高价", "最低价", "平均价"};
        for (Element tr1 : tr1s) {
            String[] strings = tr1.text().split("\\s+");// 用空白字符分隔数据
            StringBuilder productInfo = new StringBuilder();
            for (int i = 0; i < 5; ++i) {
                productInfo.append(names[i]).append(": ").append(strings[i]).append("\t");
            }
            System.out.println(productInfo);
        }

        // 获取下一页的URL
        Object nextPage = doc.selOne("//span[@id='cphRight_lblPage']/a[1]/@href");
        System.out.println(nextPage);

        // ...剩下的部分自己完成
    }

}
